$(document).ready(function(){
	//查询故事
	$.ajax({
		url: "/airbnb/getStory.do",
		async: false,
	   	type: "POST",
	   	error: function (){
	   		alert("ajax请求失败")
	   	},
	   	success: function(json){
//	   		alert(JSON.stringify(json))
	   		for(var i = 0; i < json.length; i++){
	   			var html1="<tr>";
	   			html1+="<td style='display: none;'>";
	   			html1+=json[i].stoid;
	   			html1+="</td>";
	   			html1+="<td>";
	   			html1+=json[i].title;
	   			html1+="</td>";
	   			html1+="<td>";
	   			html1+=json[i].stotype.stname;
	   			html1+="</td>"
	   			html1+="<td class='td1'>";
	   			html1+=json[i].content;
	   			html1+="</td>";
	   			html1+="<td>";
	   			html1+=json[i].pdate;
	   			html1+="</td>"
	   			html1+="<td>";
	   			html1+="<button type='button' class='btn btn-warning xiugai'>修改故事</button>";
	   			html1+="<button type='button' class='btn btn-danger shanchu'>删除</button>"
	   			html1+="</td>"
	   			html1+="</tr>"
	   			$("#tr1").append(html1);
	   		}
	   	}
    		
	});
	$("#btn_1").click(function(){
		location.href="insertgushi.html";
	})
	
	
	
	//修改故事
	//1.得到故事
	$(".xiugai").click(function(){
		var a=$(this).parent().parent().children();
		var stoid=$(a[0]).text();
		$("#divv").empty();
//		alert(stoid)
		$.ajax({
			url: "/airbnb/getStoryList.do",
			async: false,
		   	type: "POST",
			data: {"stoid":stoid},
		   	error: function (){
		   		alert("ajax请求失败")
		   	},
		   	success: function(json){
//		   		alert(JSON.stringify(json));
//		   		alert(json[0].title)
//		   		alert(json[0].stname)
		   		for(var i = 0; i < json.length; i++)
		   		{
		   			var html="<div id='div0'>";
		   				html+="<tr>"
		   				html+="<input type='hidden' id='stoid' value='";
			   			html+=json[i].stoid;
			   			html+="'/></td>";
			   			html+="</tr>"
			   			html+="<div class='div1'>";
			   			html+="<h3>添加故事</h3>";
			   			html+="</div>";
			   			html+="<div class='div1'>";
			   			html+="<input type='text' id='title'  value='"; 
						html+=json[i].title;
		   				html+="'/>";
				    	html+="</div>";
				    	html+="<div class='div1'>";
				    	html+="<span id='div1'>故事类型:</span>";
				    	html+="<select id='stotypeid'>";
				    	html+="</select>";
				    	html+="</div>";
				    	html+="<div class='div1'>";
				    	html+="<div id='div2'>";
				    	html+="<textarea id='content'>";
				    	html+=json[i].content;
						html+="</textarea>";		
				    	html+="</div>";
				    	html+="</div>";
				    	html+="<div class='div1'>";
				    	html+="<div class='layui-upload'>";
				    	html+="<button type='button' class='layui-btn' id='test2'>多图片上传</button>";
				    	html+="<blockquote class='layui-elem-quote layui-quote-nm' style='margin-top: 10px;'>";
				    	html+="预览图:";
				    	html+="<div class='layui-upload-list' id='demo2'>";
				    	html+="</blockquote>";
						html+="</div>";		
				    	html+="</div>";
				    	html+="<div id='div3'>"
				    	html+="<button type='button' class='btn btn-danger igbtn2' id='baocun'>保存</button>"
				    	html+="<button type='reset' class='btn btn-warning igbtn2' id='quxiao'>取消</button>"
				    	html+="</div>"
				    	html+="</div>"
					$("#divv").append(html);
		   		}
		   		$.ajax({
		   			url: "/airbnb/getStoryType.do",
		   			async: false,
		   		   	type: "POST",
		   		   	error: function (){
		   		   		alert("ajax请求失败")
		   		   	},
		   		   	success: function(json1){
//		   		   		alert(json[0].text)
		   		   		for(var i = 0; i < json1.length; i++){
		   		   			$("#stotypeid").append("<option value='"+json1[i].value+"'>"+json1[i].text+"</option>")
		   		   		}
		   		   	}
		   		});
		   		   		
		   		
		   		layui.use('upload', function(){
		   		  	var $ = layui.jquery
		   		  	,upload = layui.upload; 
		   		  //多图片上传
		   		  upload.render({
		   		    elem: '#test2'
		   		    ,url: '/airbnb/insertimg.do'
		   		    ,multiple: true
		   		    ,before: function(obj){
		   		      //预读本地文件示例，不支持ie8
		   		      obj.preview(function(index, file, result){
		   		        $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
		   		      });
		   		    }
		   		    ,done: function(res){
		   		      //上传完毕
		   		    	
		   		      console.log(res)
		   		    }
		   		  });
		   		});
		   		
		   	}
	    		
		});
	})
	
	$(document).on("click","#baocun",function (){		
		var title=$("#title").val();
		var stotypeid=$("#stotypeid").val();
		var content=$("#content").val();
		var stoid=$("#stoid").val();
		$.ajax({
   			url: "/airbnb/updateStory.do",
   			async: false,
   		   	type: "POST",
   		   	data:{"stoid":stoid,"title":title,"stotypeid":stotypeid,"content":content},
   		   	error: function (){
   		   		alert("ajax请求失败")
   		   	},
   		   	success: function(json){
//   		   	alert(JSON.stringify(json));
   		   		alert("保存成功")
   		   		location.href="guanlistory.html";
   		   	}
   		});
		
	})
	
	//取消
	$(document).on("click","#quxiao",function (){		
		window.location.href="guanlistory.html";
	})
	//保存
	$(document).on("click","#fabu",function (){		
		
		window.location.href="guanlistory.html";
	})
	
	
	$(".shanchu").click(function(){
		var b=$(this).parent().parent().children();
		var x=$(b[0]).text();
		
		$.ajax({
   			url: "/airbnb/deleteStory.do",
   			async: false,
   		   	type: "POST",
   		   	data: {"stoid":x},
   		   	error: function (){
   		   		alert("ajax请求失败")
   		   	},
   		   	success: function(json){
   		   		window.location.href="guanlistory.html";
   		   	}
   		})
	})
	
})
